//
//  BreatheChartView.m
//  JiuLe
//
//  Created by 黄鑫 on 16/9/12.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "BreatheChartView.h"

@implementation BreatheChartView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        viewH                = [UIScreen mainScreen].bounds.size.width ;
        viewW                = [UIScreen mainScreen].bounds.size.width;
        self.chartH      =        viewH - 210;
        self.chartw      =        viewW - 40;
    }
    return self;
}

- (CGRect)getGraphicPositionget:(NSString *)number index:(int)i{
    float yCoordinate ;
    if([number isEqualToString:@"0"]){
        yCoordinate  = self.chartY + self.chartH;
    }else{
        yCoordinate  = self.chartY + self.chartH - ([number intValue]*self.percentage);//5 为单位比例yInterval/每条线代表的单位
    }
    float xCoordinate;
    if(self.baseChartShowDate == BaseChartShowDateDay){
        xCoordinate = self.chartX + (i * (self.chartw/288.0));
    }else if (self.baseChartShowDate == BaseChartShowDateWeek){
        xCoordinate = self.chartX + (i * (self.chartw/6));
    }else{
        xCoordinate = self.chartX + (i * (self.chartw/28));
    }

    if(CGRectGetWidth(self.frame) > 320){
        if(CGRectGetWidth(self.frame) > 375){
            if(self.baseChartType == BaseChartTypeLineChart){
                return CGRectMake(xCoordinate - (CircleDiameter/2), yCoordinate - CircleDiameter-1, CircleDiameter, CircleDiameter);
            }else{
                return CGRectMake(xCoordinate - (RectangleWidth/2), yCoordinate - CircleDiameter+1, RectangleWidth, self.chartY + self.chartH - yCoordinate);
            }
        }else{
            if(self.baseChartType == BaseChartTypeLineChart){
                return CGRectMake(xCoordinate - (CircleDiameter/2), yCoordinate - (CircleDiameter/2) - 0.4, CircleDiameter, CircleDiameter);
            }else{
                return CGRectMake(xCoordinate - (RectangleWidth/2), yCoordinate - 0.3, RectangleWidth, self.chartY + self.chartH - yCoordinate);
            }
        }
    }else{
        if(self.baseChartType == BaseChartTypeLineChart){
            return CGRectMake(xCoordinate - (CircleDiameter/2), yCoordinate - (CircleDiameter/2), CircleDiameter, CircleDiameter);
        }else{
            return CGRectMake(xCoordinate - (RectangleWidth/2), yCoordinate, RectangleWidth, self.chartY + self.chartH - yCoordinate);
        }
    }
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    CGRect shadow = CGRectZero;
    float yCoordinate  = self.chartY + self.chartH - (self.shadowStart*self.percentage);
    float yCoordinate2  = self.chartY + self.chartH - (self.shadowEnd*self.percentage);
    shadow = CGRectMake(self.chartX, yCoordinate2, self.chartw, yCoordinate - yCoordinate2);
    [self toDrawRect:shadow color:[UIColor colorWithRed:103/255.0 green:207/255.0 blue:236/255.0 alpha:0.3] context:context];
    
    UIFont *font = [UIFont systemFontOfSize:10];//设置字体
    UIColor *color = [UIColor blackColor];//字体颜色
    NSMutableParagraphStyle *style=[[NSMutableParagraphStyle alloc]init];//段落样式
    NSTextAlignment align = NSTextAlignmentRight;//对齐方式
    style.alignment = align;
    NSString *textStart = [NSString stringWithFormat:@"%d",self.shadowStart];
    if ([textStart isEqualToString:@"360"]) {//睡眠图
        textStart = @"6h";
        [textStart drawInRect:CGRectMake(5, yCoordinate - 5, 20, 21) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
        NSString *textEnd = @"8h";
        [textEnd drawInRect:CGRectMake(5, yCoordinate2 -5, 20, 21) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
    }else{
        [textStart drawInRect:CGRectMake(-2, yCoordinate - 5, 20, 21) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
        NSString *textEnd = [NSString stringWithFormat:@"%d",self.shadowEnd];
        [textEnd drawInRect:CGRectMake(-2, yCoordinate2 -5, 20, 21) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
    }
}


@end
